﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace DeloUtils.Migra
{
    // Этот клас необходим для удобства миграции
    class ExportTables
    {
        public static LocalDBCompact localdb;
        public static event ProgressHandler CurrentRows;
        public static event ProgressMessage CurrentMessage;
        public static int CountTask = 1;

        public static void Execute(OleDbConnection connection)
        {
            PatchTables(connection);
            ExportDepartments(connection);
            ExportDocgroups(connection);
            ExportRubrics(connection);
            ExportSecurity(connection);
            ExportDelivery(connection);
            ExportCabinets(connection);
            ExportOrganiz(connection);
            ExportCitizen(connection);
            //ExportProt(connection);
            ExportAr(connection);
            CountTask = 1;
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Завершено!");
        }
        private static void PatchTables(OleDbConnection connection)
        {
            try
            {
                String sql = ""
                           + "begin tran"
                           + " create table #t_user_var(uisn integer, uname varchar(64), reg char(1) null,lisn integer null,loper char(6) null,flags varchar(255) default ',',flags_evnt varchar(max) default ',',prot_flags varchar(255) default ',')"
                           + " insert into #t_user_var(uisn) values (0)"
                           + " alter table DOC_RC disable trigger TR_IU_DOC_RC"
                           + " alter table DOC_RC disable trigger TR_F_DOC_RC"
                           + " alter table RESOLUTION disable trigger TR_IUD_RESOLUTION"
                           + " update ORGANIZ_CL set CLASSIF_NAME = REPLACE(CLASSIF_NAME,'''','`') where  (CLASSIF_NAME LIKE '%' + CHAR(39) + '%')"
                           + " update ORGANIZ_CL set FULLNAME = REPLACE(FULLNAME,'''','`') where  (FULLNAME LIKE '%' + CHAR(39) + '%')"
                           + " update ORGANIZ_CL set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update ORGANIZ_CL set ADDRESS = REPLACE(ADDRESS,'''','`') where  (ADDRESS LIKE '%' + CHAR(39) + '%')"
                           + " update ORGANIZ_CL set CITY = REPLACE(CITY,'''','`') where  (CITY LIKE '%' + CHAR(39) + '%')"
                           + " update DEPARTMENT set CLASSIF_NAME = REPLACE(CLASSIF_NAME,'''','`') where  (CLASSIF_NAME LIKE '%' + CHAR(39) + '%')"
                           + " update DEPARTMENT set SURNAME = REPLACE(SURNAME,'''','`') where  (SURNAME LIKE '%' + CHAR(39) + '%')"
                           + " update DEPARTMENT set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update DEPARTMENT set DUTY = REPLACE(DUTY,'''','`') where  (DUTY LIKE '%' + CHAR(39) + '%')"
                           + " update DELIVERY_CL set CLASSIF_NAME = REPLACE(CLASSIF_NAME,'''','`') where  (CLASSIF_NAME LIKE '%' + CHAR(39) + '%')"
                           + " update LINK_CL set CLASSIF_NAME = REPLACE(CLASSIF_NAME,'''','`') where  (CLASSIF_NAME LIKE '%' + CHAR(39) + '%')"
                           + " update CITIZEN set CITIZEN_SURNAME = REPLACE(CITIZEN_SURNAME,'''','`') where  (CITIZEN_SURNAME LIKE '%' + CHAR(39) + '%')"
                           + " update CITIZEN set CITIZEN_CITY = REPLACE(CITIZEN_CITY,'''','`') where  (CITIZEN_CITY LIKE '%' + CHAR(39) + '%')"
                           + " update CITIZEN set CITIZEN_ADDR = REPLACE(CITIZEN_ADDR,'''','`') where  (CITIZEN_ADDR LIKE '%' + CHAR(39) + '%')"
                           + " update CITIZEN set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update DOC_RC set ANNOTAT = REPLACE(ANNOTAT,'''','`') where  (ANNOTAT LIKE '%' + CHAR(39) + '%')"
                           + " update DOC_RC set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update DOC_RC set FREE_NUM = REPLACE(FREE_NUM,'''','`') where  (FREE_NUM LIKE '%' + CHAR(39) + '%')"
                           + " update REPLY set REPLY_TEXT = REPLACE(REPLY_TEXT,'''','`') where  (REPLY_TEXT LIKE '%' + CHAR(39) + '%')"
                           + " update REPLY set REPLY_TEXT_1 = REPLACE(REPLY_TEXT_1,'''','`') where  (REPLY_TEXT_1 LIKE '%' + CHAR(39) + '%')"
                           + " update REPLY set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update RESOLUTION set RESOLUTION_TEXT = REPLACE(RESOLUTION_TEXT,'''','`') where  (RESOLUTION_TEXT LIKE '%' + CHAR(39) + '%')"
                           + " update RESOLUTION set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update RESOLUTION set SUMMARY = REPLACE(SUMMARY,'''','`') where  (SUMMARY LIKE '%' + CHAR(39) + '%')"
                           + " update RESOLUTION set [RESUME] = REPLACE([RESUME],'''','`') where  ([RESUME] LIKE '%' + CHAR(39) + '%')"
                           + " update REF_CORRESP set CORRESP_NUM = REPLACE(CORRESP_NUM,'''','`') where  (CORRESP_NUM LIKE '%' + CHAR(39) + '%')"
                           + " update REF_CORRESP set ANNOTAT = REPLACE(ANNOTAT,'''','`') where  (ANNOTAT LIKE '%' + CHAR(39) + '%')"
                           + " update REF_CORRESP set ANNOTAT_1 = REPLACE(ANNOTAT_1,'''','`') where  (ANNOTAT_1 LIKE '%' + CHAR(39) + '%')"
                           + " update REF_CORRESP set NOTE = REPLACE(NOTE,'''','`') where  (NOTE LIKE '%' + CHAR(39) + '%')"
                           + " update REF_CORRESP set NOTE_1 = REPLACE(NOTE_1,'''','`') where  (NOTE_1 LIKE '%' + CHAR(39) + '%')"
                           + " update REF_CORRESP set CORRESP_SIGN = REPLACE(CORRESP_SIGN,'''','`') where  (CORRESP_SIGN LIKE '%' + CHAR(39) + '%')"
                           + " alter table DOC_RC enable trigger TR_IU_DOC_RC"
                           + " alter table DOC_RC enable trigger TR_F_DOC_RC"
                           + " alter table RESOLUTION enable trigger TR_IUD_RESOLUTION"
                           + " commit"
                           + ""
                               ;
                OleDbCommand cmd = new OleDbCommand(sql, connection);
                int patchres = cmd.ExecuteNonQuery();

                System.Windows.Forms.MessageBox.Show("Выполнение завершено. Исправлено: " + patchres.ToString() + " кавычек");
            }
            catch (Exception ex)
            {

                throw new Exception("PatchTables || " + ex.Message);
            }
        }

        private static void ExportDepartments(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM DEPARTMENT";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask=(int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Подразделений");

            sql1 = "SELECT ISN_NODE,ISN_HIGH_NODE,DUE,IS_NODE,CLASSIF_NAME,SURNAME,DUTY,DELETED,CARD_FLAG,CARD_NAME FROM DEPARTMENT";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i=1;
            while (dr.Read())
            {
                System.Windows.Forms.Application.DoEvents();

                sql2 = "INSERT INTO DEPARTMENT (ISN_NODE,ISN_HIGH_NODE,DUE,IS_NODE,CLASSIF_NAME,SURNAME,DUTY,DELETED,CARD_FLAG,CARD_NAME) VALUES "
                    + "(" + dr[0].ToString() + "," + (dr.IsDBNull(1) ? "0" : dr[1].ToString()) + ",'" + dr[2].ToString() + "'," + dr[3].ToString() + ",'" + dr[4].ToString() + "','"
                + dr[5].ToString() + "','" + dr[6].ToString() + "'," + dr[7].ToString() + "," + (dr.IsDBNull(8) ? "0" : dr[8].ToString()) + ",'" + dr[9].ToString() + "')";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        private static void ExportDocgroups(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM DOCGROUP_CL";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Групп документов");

            sql1 = "SELECT ISN_NODE,ISN_HIGH_NODE,DUE,IS_NODE,CLASSIF_NAME,DELETED FROM DOCGROUP_CL";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                sql2 = "INSERT INTO DOCGROUP (ISN_NODE,ISN_HIGH_NODE,DUE,IS_NODE,CLASSIF_NAME,DELETED) VALUES "
                    + "(" + dr[0].ToString() + "," + (dr.IsDBNull(1) ? "0" : dr[1].ToString()) + ",'" + dr[2].ToString() + "'," + dr[3].ToString() + ",'" + dr[4].ToString() + "'," + dr[5].ToString() + ")";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        private static void ExportRubrics(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM RUBRIC_CL";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Рубрик");

            sql1 = "SELECT DUE,ISN_NODE,ISN_HIGH_NODE,LAYER,IS_NODE,CLASSIF_NAME,DELETED,RUBRIC_CODE FROM RUBRIC_CL";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                sql2 = "INSERT INTO RUBRIC_CL (DUE,ISN_NODE,ISN_HIGH_NODE,LAYER,IS_NODE,CLASSIF_NAME"
                     + ",DELETED,RUBRIC_CODE) VALUES "
                     + "('" + dr[0].ToString() + "'," + dr[1].ToString() + "," + (dr.IsDBNull(2) ? "0" : dr[2].ToString())
                     + "," + dr[3].ToString() + "," + dr[4].ToString() + ",'" + dr[5].ToString()
                     + "'," + dr[6].ToString() + ",'" + dr[7].ToString() + "')";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        private static void ExportSecurity(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM SECURITY_CL";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Видов доступа");

            sql1 = "SELECT SECURLEVEL,GRIF_NAME FROM SECURITY_CL";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                sql2 = "INSERT INTO SECURITY (SECURLEVEL,GRIF_NAME) VALUES "
                    + "(" + dr[0].ToString() + ",'" + dr[1].ToString() + "')";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        private static void ExportDelivery(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM DELIVERY_CL";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Видов доставки");

            sql1 = "SELECT ISN_LCLASSIF,CLASSIF_NAME,PROTECTED,DELETED FROM DELIVERY_CL";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                sql2 = "INSERT INTO DELIVERY_CL (ISN_LCLASSIF,CLASSIF_NAME,PROTECTED,DELETED) VALUES "
                    + "(" + dr[0].ToString() + ",'" + dr[1].ToString() + "'," + dr[2].ToString() + "," + dr[3].ToString()+")";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);

            }
        }
        private static void ExportCabinets(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM CABINET";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Кабинетов");

            sql1 = "SELECT ISN_CABINET,DUE,CABINET_NAME FROM CABINET";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                sql2 = "INSERT INTO CABINET (ISN_CABINET,DUE,CABINET_NAME) VALUES "
                    + "(" + dr[0].ToString() + ",'" + dr[1].ToString() + "','" + dr[2].ToString() + "')";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        private static void ExportOrganiz(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM ORGANIZ_CL";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Организаций");

            sql1 = "SELECT DUE,ISN_NODE,ISN_HIGH_NODE,IS_NODE,CLASSIF_NAME"
                      + ",PROTECTED,DELETED,ZIPCODE,ADDRESS,OKPO,INN,ISN_REGION,OKONH,LAW_ADRESS FROM ORGANIZ_CL";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                try
                {
                    sql2 = "INSERT INTO ORGANIZ_CL (DUE,ISN_NODE,ISN_HIGH_NODE,IS_NODE,CLASSIF_NAME"
                         + ",PROTECTED,DELETED,ZIPCODE,ADDRESS,OKPO,INN,ISN_REGION,OKONH,LAW_ADRESS "
                         + ") VALUES "
                         + "('" + dr[0].ToString() + "'," + dr[1].ToString() + "," + (dr.IsDBNull(2) ? "0" : dr[2].ToString()) + "," + dr[3].ToString() + ",'"
                         + dr[4].ToString() + "'," + dr[5].ToString() + "," + dr[6].ToString() + "," + (dr.IsDBNull(7) ? "0" : "'" + dr[7].ToString() + "'") + ","
                         + (dr.IsDBNull(8) ? "0" : "'" + dr[8].ToString() + "'") + "," + (dr.IsDBNull(9) ? "0" : "'" + dr[9].ToString() + "'") + ","
                         + (dr.IsDBNull(10) ? "0" : dr[10].ToString()) + "," + (dr.IsDBNull(11) ? "0" : "'" + dr[11].ToString() + "'") + ","
                         + (dr.IsDBNull(12) ? "0" : "'" + dr[12].ToString() + "'") + "," + (dr.IsDBNull(13) ? "0" : "'" + dr[13].ToString() + "'") + ")";
                    cmd2.CommandText = sql2;
                    cmd2.ExecuteNonQuery();
                    CurrentRows(i++);
                }
                catch (Exception ex )
                {
                    System.IO.File.AppendAllText("organiz.log", sql2 + "\n" + ex.Message);
                    continue;
                }
            }
        }
        private static void ExportCitizen(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM CITIZEN";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Граждан");

            sql1 = "SELECT ISN_CITIZEN,CITIZEN_SURNAME,CITIZEN_CITY"
                        + ",ZIPCODE,CITIZEN_ADDR,DELETED,PROTECTED,ISN_ADDR_CATEGORY FROM CITIZEN";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                sql2 = "INSERT INTO CITIZEN (ISN_CITIZEN,CITIZEN_SURNAME,CITIZEN_CITY"
                     + ",ZIPCODE,CITIZEN_ADDR,DELETED,PROTECTED) VALUES "
                    + "(" + dr[0].ToString() + "," + (dr.IsDBNull(1) ? "0" : "'" + dr[1].ToString() + "'") + "," + (dr.IsDBNull(2) ? "0" : "'" + dr[2].ToString() + "'") 
                    + "," + (dr.IsDBNull(3) ? "0" : "'" + dr[3].ToString() + "'") + "," + (dr.IsDBNull(4) ? "0" : "'" + dr[4].ToString() + "'") + "," + dr[5].ToString()
                    + "," + dr[6].ToString() + ")";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        public static void ExportProt(OleDbConnection connection)
        {
            String sql1 = "SELECT COUNT(*) FROM PROT";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            CountTask = (int)cmd1.ExecuteScalar();
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Протоколов работы с документами");
            
            sql1 = "SELECT TABLE_ID,OPER_ID,SUBOPER_ID,OPER_DESCRIBE,REF_ISN,TIME_STAMP,USER_ISN,OPER_COMMENT FROM PROT";
            cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            int i = 1;
            while (dr.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                sql2 = "INSERT INTO PROT (TABLE_ID,OPER_ID,SUBOPER_ID,OPER_DESCRIBE,REF_ISN,TIME_STAMP,USER_ISN,OPER_COMMENT) VALUES "
                    + "('" + dr[0].ToString() + "','" + dr[1].ToString() + "','" + dr[2].ToString() + "','" + dr[3].ToString() + "'," + dr[4].ToString()
                    + ",'" + dr[5].ToString() + "'," + dr[6].ToString() + ",'" + dr[7].ToString() + "')";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i++);
            }
        }
        public static void ExportAr(OleDbConnection connection)
        {
            CurrentMessage("Состояние выгрузки: Выгрузка справочников - Дополнительных реквизитов");
            String sql1 = "SELECT * FROM AR_RC_VALUE";
            OleDbCommand cmd1 = new OleDbCommand(sql1, connection);
            OleDbDataReader dr = cmd1.ExecuteReader();
            String sql2 = "";
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = localdb.Connection;
            CountTask = dr.FieldCount;
            for (int i = 2; i < CountTask; i++)
			{
                sql2 = "INSERT INTO PROTOCOL (TNAME,EXPORTDUE,EXPORTISN) VALUES "
                    + "('AR','" + dr.GetName(i) + "',"+i.ToString()+")";
                cmd2.CommandText = sql2;
                cmd2.ExecuteNonQuery();
                CurrentRows(i-2);			 
			}
            cmd2.Dispose();
            dr.Close();
        }

        public static void ExportFile(OleDbConnection connection, String Folder)
        {
            if (connection == null) return;
            CurrentMessage("Состояние выгрузки: Выгрузка файлов");
            string sql = "SELECT COUNT(ctx.ISN_REF_FILE) FROM FILE_CONTENTS2 ctx INNER JOIN REF_FILE ref ON ctx.ISN_REF_FILE = ref.ISN_REF_FILE";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            CountTask = (int)cmd.ExecuteScalar();
            cmd.Dispose();
            sql = "SELECT ctx.CONTENTS, ref.ISN_REF_DOC, ref.NAME, ref.DESCRIPTION, ref.CATEGORY, ref.FILESIZE "+
                         "FROM FILE_CONTENTS2 ctx INNER JOIN " +
                         "REF_FILE ref ON ctx.ISN_REF_FILE = ref.ISN_REF_FILE";
            cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            string path = "";
            byte[] data = null;
            byte[] data2 = null;
            long size = 0;
            int size_db = 0;
            int i = 0;
            while (dr.Read())
            {
                path = Folder + "\\" + dr.GetInt32(1).ToString();
                if (!dr.IsDBNull(4)) path += "\\" + dr.GetValue(4).ToString();
                System.IO.Directory.CreateDirectory(path);
                if (dr.IsDBNull(5)) size_db = 10*1024*1024;
                else size_db = dr.GetInt32(5) * 1024;
                data = new byte[size_db];
                size = dr.GetBytes(0, 0, data, 0, size_db);
                data2 = new byte[size];
                for (int j = 0; j < size; j++) data2[j] = data[j];
                data = null;
                CurrentMessage("Состояние выгрузки: Выгрузка файлов - " + dr.GetString(3) + "[" + ((int)(size/1024)).ToString() + "Kb]");
                CurrentRows(++i);	
                System.IO.File.WriteAllBytes(path + "\\" + dr.GetString(3),data2);
                data2 = null;
            }
            dr.Close();
        }
    }
}
